package model

import (
	"encoding/json"
	"time"
	"todo-list/config"
	"todo-list/constants"
)

type TaskStatus int

const (
	NotStart TaskStatus = iota + 1
	Abandon
	Doing
	Done
)

func (s TaskStatus) MarshalJSON() ([]byte, error) {
	m := make(map[string]any)
	m["name"] = config.TranslateTaskStatus(s.String())
	m["code"] = int(s)
	return json.Marshal(m)
}

func (s TaskStatus) String() string {
	switch s {
	case NotStart:
		return constants.MsgTaskNotStart
	case Abandon:
		return constants.MsgTaskAbandon
	case Doing:
		return constants.MsgTaskDoing
	case Done:
		return constants.MsgTaskDone
	default:
		return ""
	}
}

func (s TaskStatus) Validate() bool {
	return s >= NotStart && s <= Done
}

type Task struct {
	BaseModel
	UserId      uint       `gorm:"size:16"`
	Title       string     `gorm:"not null;size:128"`
	Status      TaskStatus `gorm:"default:1;size:2"`
	Public      bool       `gorm:"default:false"`
	Content     string
	StartTime   *time.Time
	EndTime     *time.Time
	AbandonTime *time.Time
}

func NewTask(userId uint, title string, content string, status TaskStatus, public bool) *Task {
	return &Task{UserId: userId, Title: title, Content: content, Status: status, Public: public}
}
